package com.mall.shopping.services;

import com.mall.shopping.MyProductService;
import com.mall.shopping.constants.ShoppingRetCode;
import com.mall.shopping.converter.MyProductConvertor;
import com.mall.shopping.dal.entitys.Item;
import com.mall.shopping.dal.persistence.ItemMapper;
import com.mall.shopping.dto.MyProductDTO;
import com.mall.shopping.dto.QueryItemByIdRequest;
import com.mall.shopping.dto.QueryItemByIdResponse;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@Service(interfaceClass = MyProductService.class)
public class MyProductServiceImpl implements MyProductService {


    @Autowired
    private ItemMapper itemMapper;

    @Autowired
    private MyProductConvertor productConvertor;


    @Override
    public QueryItemByIdResponse  queryItemById(QueryItemByIdRequest request) {

        QueryItemByIdResponse response = new QueryItemByIdResponse();

        try {
            request.requestCheck();
        }
        catch (RuntimeException e) {

            response.setCode(ShoppingRetCode.SYSTEM_ERROR.getCode());
            response.setMsg(ShoppingRetCode.SYSTEM_ERROR.getMessage());
            return response;
        }


        Item item = itemMapper.selectByPrimaryKey(request.getProductId());

        // MapStruct
        MyProductDTO productDTO = productConvertor.convert2DTO(item);



        response.setProductDTO(productDTO);
        response.setCode(ShoppingRetCode.SUCCESS.getCode());
        response.setMsg(ShoppingRetCode.SUCCESS.getMessage());

        return response;



    }
}
